﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;

namespace BarberShop.BLL
{
    public class MovieInfoBLL
    {

        public static IList<MoveInfo> GetMovieList(string name)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<MoveInfo, bool>> WhereExpr = PredicateExtensions.True<MoveInfo>();
                if (!string.IsNullOrEmpty(name))
                {
                    WhereExpr = WhereExpr.And(x => x.MoveName.Contains(name));
                }
                var Move = data.MoveInfos.Where(WhereExpr);
                return Move.ToList();
            }
        }
        /// <summary>
        /// 添加影片
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddMovieInfo(MoveInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.MoveInfos.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MoveInfo GetMoveInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.MoveInfos where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        public static bool UpdateMoveInfo(MoveInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.MoveInfos where a.ID == info.ID select a).FirstOrDefault();
                    r.CreateDate = info.CreateDate;
                    r.FileName = info.FileName;
                    r.ID = info.ID;
                    r.MoveName = info.MoveName;
                    r.Size = info.Size;
                    r.TimeLength = info.TimeLength;
                    r.User_ID = info.User_ID;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
    }
}
